using Warehouse.Domain.Entities;
using SqlSugar;
using System.Collections.Generic;

namespace Warehouse.Infrastructure.Repositories
{
    public class WarehouseModelRepository<T> : IWarehouseModelRepository<T> where T : BaseEntity , new()
    {
        private readonly ISqlSugarClient _db;
        public WarehouseModelRepository(ISqlSugarClient db)
        {
            _db = db;
        }

        /// <summary>
        /// 创建模型到数据库
        /// </summary>
        /// <param name="model">要创建的模型</param>
        /// <returns>执行插入操作影响的行数</returns>
        public int Create(T model)
        {
            if (model == null)
                throw new ArgumentNullException(nameof(model));

            try
            {
                return _db.Insertable(model).ExecuteCommand();
            }
            catch (Exception ex)
            {
                // 记录日志并抛出统一异常
                Console.WriteLine($"Error creating model: {ex.Message}");
                throw;
            }
        }

        /// <summary>
        /// 根据ID获取模型
        /// </summary>
        /// <param name="id">模型的唯一标识符</param>
        /// <returns>相应的模型实例，如果未找到则返回null</returns>
        public T GetModel(long id)
        {
            if (id <= 0)
                throw new ArgumentOutOfRangeException(nameof(id), "ID 不能小于或等于 0");

            try
            {
                return _db.Queryable<T>().InSingle(id);
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error fetching model with ID {id}: {ex.Message}");
                throw;
            }
        }

        /// <summary>
        /// 获取只读不跟踪的查询对象
        /// </summary>
        /// <returns>用于只读操作的查询对象</returns>
        public ISugarQueryable<T> GetQuery()
        {
            try
            {
                return _db.Queryable<T>(); // 使用缓存提高性能
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error creating read-only query: {ex.Message}");
                throw;
            }
        }

        /// <summary>
        /// 获取所有模型的列表
        /// </summary>
        /// <returns>所有符合的模型实体列表</returns>
        public List<T> GetValue()
        {
            try
            {
                // 使用缓存提高性能
                return _db.Queryable<T>().ToList();
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error fetching all values: {ex.Message}");
                throw;
            }
        }

        /// <summary>
        /// 更新模型到数据库
        /// </summary>
        /// <param name="model">要更新的模型</param>
        /// <returns>执行更新操作影响的行数</returns>
        public int Update(T model)
        {
            if (model == null)
                throw new ArgumentNullException(nameof(model));

            try
            {
                return _db.Updateable(model).ExecuteCommand();
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error updating model: {ex.Message}");
                throw;
            }
        }
        
      
       
    }
} 